var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: '请选择',
    switch1Checked:true,
    city_id:'',
    area_id:'',
    saveblen:false,
    sele_adress: [],
    addressisnum:1,
    biaoqian: [
      { value: "家", blen: false }, { value: "父母家", blen: false }, { value: "公司", blen: false }, { value: "学校", blen: false }
    ],
    adressInfoDataList: [
      // {
      //   id: 1,
      //   checked: true,
      //   name: "张三",
      //   gender: "先生",
      //   phone: "18634562235",
      //   adressName: "德容科技大厦",
      //   houseNumber: "2楼三单元5034",
      //   detailedAddress: "",
      //   label: "家"
      // }, {
      //   id: 2,
      //   checked: false,
      //   name: "李四",
      //   gender: "女士",
      //   phone: "18634564444",
      //   adressName: "北京师范大学",
      //   houseNumber: "100楼34单元6664",
      //   detailedAddress: "",
      //   label: "学校"
      // }
    ]
  },
  //省市区选择器：
  bindRegionChange: function (e) {
//console.log(e,9999999);
//console.log('picker发送选择改变，携带值为', e.detail.value)
    // let res = e.detail.value;
    this.data.city_id = e.detail.code[0],
    this.data.area_id = e.detail.code[1],
    this.data.region = e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2];
    this.data.sele_adress.area_info = this.data.region;
    this.setData({
      region: this.data.region,
      sele_adress: this.data.sele_adress
    })
  },
  onChange(e) {
    let that = this;
//console.log(e.currentTarget.dataset.prop, e.detail);
    that.data.sele_adress.true_name = e.detail.value;
    that.setData({
      sele_adress: that.data.sele_adress
    })
  },
  bindKeyInput(e) {
    let that = this;
//console.log(e.currentTarget.dataset.prop, e.detail);
    that.data.sele_adress.mob_phone = e.detail.value;
    that.setData({
      sele_adress: that.data.sele_adress
    })
  },
  bindKeyaddress(e) {
    let that = this;
//console.log(e.currentTarget.dataset.prop, e.detail);
    that.data.sele_adress.address = e.detail.value;
    that.setData({
      sele_adress: that.data.sele_adress
    })
  },
  // add_biaoqian(e){
  //   console.log(e.target.dataset.ind);
  //   let that = this;
  //   let ind = e.target.dataset.ind;
  //   for (let i = 0; i < that.data.biaoqian.length;i++){
  //     that.data.biaoqian[i].blen = false;
  //   }
  //   that.data.biaoqian[ind].blen = true;
  //   let val = that.data.biaoqian[ind].value;
  //   console.log(val);
  //   that.setData({
  //     biaoqian: that.data.biaoqian
  //   });
  // },
  switch1Change(e){
    let that = this;
    let blen = e.detail.value;
//console.log(blen);
    if (blen){
//console.log(1);
      that.data.addressisnum = 1;
    }else{
      that.data.addressisnum = 0;
//console.log('0');
    }
  },
  // go_address_location(){
  //   wx.navigateTo({
  //     url: '../../pages/address_location/address_location'
  //   })
  // },
  //编辑接口index.php?act=member_address&op=address_edit
  address_edit(){
    let that = this;
    let k = wx.getStorageSync('key');
    if (that.data.switch1Checked) {
      that.data.sele_adress.is_default = 1;
    } else {
      that.data.sele_adress.is_default = 0;
    }
console.log('地址编辑:', that.data.sele_adress);
    wx.request({
      url: ApiUrl + '/index.php?act=member_address&op=address_edit',
      data: {
        key:k,
        address_id: that.data.sele_adress.address_id,//地址编号
        true_name: that.data.sele_adress.true_name,//姓名
        city_id: that.data.sele_adress.city_id,//城市编号(地址联动第二级)
        area_id: that.data.sele_adress.area_id, //地区编号(地址联动最后一级)
        area_info: that.data.region, //地区信息，例：天津 天津市 红桥区
        address: that.data.sele_adress.address, //地址信息，例：水游城8层
        tel_phone: that.data.sele_adress.tel_phone, //电话号码
        mob_phone: that.data.sele_adress.mob_phone,//手机
        is_default: that.data.sele_adress.is_default, //是否为默认收货地址 1是 0否
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",//get为默认方法/POST
      success: function (result) {
        if (result.data.code==200) {
          that.go_to_();
        }
//console.log('地址编辑：', result.data.datas);
      }
    });
  },
  //地址添加 index.php?act=member_address&op=address_add
  address_add() {
    let that = this;
//console.log('地址添加:', that.data.sele_adress);
    let k = wx.getStorageSync('key');
    if (that.data.switch1Checked){
      that.data.sele_adress.is_default = 1;
    }else{
      that.data.sele_adress.is_default = 0;
    }
    wx.request({
      url: ApiUrl + '/index.php?act=member_address&op=address_add',
      data: {
        key: k,
        true_name: that.data.sele_adress.true_name,//姓名
        city_id: that.data.city_id,//城市编号(地址联动第二级)
        area_id: that.data.area_id, //地区编号(地址联动最后一级)
        area_info: that.data.region, //地区信息，例：天津 天津市 红桥区
        address: that.data.sele_adress.address, //地址信息，例：水游城8层
        tel_phone: that.data.sele_adress.tel_phone, //电话号码
        mob_phone: that.data.sele_adress.mob_phone,//手机
        is_default: that.data.sele_adress.is_default, //是否为默认收货地址 1是 0否
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",//get为默认方法/POST
      success: function (result) {
        if (result.data.code==200) {
          that.go_to_();
        }
//console.log('地址添加:', result.data.datas);
      }
    });
  },
  saveBtn(){
    let that = this;
    if (that.data.saveblen){
      that.address_edit();
//console.log('地址编辑==保存')
    } else {
      that.address_add();
//console.log('地址添加==保存')
    }
  },
  //删除地址index.php?act=member_address&op=address_del
  deleBtn() {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_address&op=address_del',
      data: {
        key: k,
        address_id: that.data.sele_adress.address_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",//get为默认方法/POST
      success: function (result) {
//console.log('地址添加:', result.data.datas);
        if (result.data.code==200){
          that.go_to_();
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    
    let that = this;
    that.data.saveblen = false;
    that.data.adressInfoDataList = wx.getStorageSync('adressInfoData');
     if (options.id){
       that.data.saveblen = true;
      let ID = options.id;

      for (let i = 0; i < that.data.adressInfoDataList.length; i++) {
        if (ID == that.data.adressInfoDataList[i].address_id) {
          that.data.sele_adress=(that.data.adressInfoDataList[i]);
        }
      }
      that.setData({
        sele_adress: that.data.sele_adress,
        region: that.data.sele_adress.area_info
      });
     } else {
       that.data.saveblen = false;
       let se_adress = {
         true_name: '',//姓名
         // city_id: that.data.sele_adress.city_id, //城市编号(地址联动第二级)
         // area_id: that.data.sele_adress.area_id, //地区编号(地址联动最后一级)
         // area_info: that.data.sele_adress.area_info, //地区信息，例：天津 天津市 红桥区
         address: '', //地址信息，例：水游城8层
         tel_phone: '', //电话号码
         mob_phone: '',//手机
         is_default: '0', //是否为默认收货地址 1是 0否
         region: ''
       }
       that.setData({
         sele_adress: se_adress
       });
    }

//console.log(options, 90909090, that.data.sele_adress, that.data.adressInfoDataList);
   
  },
  go_to_:function(){
    
        let pages = getCurrentPages();
        let beforePage = pages[pages.length - 2];
          wx.navigateBack({//返回
            delta: 1
          })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})